package org.aaron.mp3player;

import org.aaron.mp3player.model.Mp3Info;
import org.aaron.mp3player.service.Mp3PlayerService;
import org.aaron.mp3player.utils.SystemConfig;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class Mp3PlayerActivity extends Activity {
	private ImageButton start_btn, pause_btn, stop_btn;
	private Mp3Info mp3Info = null;
	private TextView lrcview = null;
	private BroadcastReceiver receiver=null;
	private IntentFilter filter=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.mp3playerlayout);
		Intent intent = getIntent();
		mp3Info = (Mp3Info) intent.getSerializableExtra("mp3info");
		initActivity();
	}

	@Override
	protected void onResume() {
		receiver=new lrcMessageBroadcastReceiver();
		registerReceiver(receiver, getIntentFilter());
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		unregisterReceiver(receiver);
		super.onPause();
	}
	
	private void initActivity() {
		start_btn = (ImageButton) this.findViewById(R.id.start_btn);
		pause_btn = (ImageButton) this.findViewById(R.id.pause_btn);
		stop_btn = (ImageButton) this.findViewById(R.id.stop_btn);
		start_btn.setOnClickListener(new StartClickListener());
		pause_btn.setOnClickListener(new PauseClickListener());
		stop_btn.setOnClickListener(new StopClickListener());
		lrcview = (TextView) this.findViewById(R.id.lrcview);
	}

	class StartClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(Mp3PlayerActivity.this, Mp3PlayerService.class);
			intent.putExtra("mp3info", mp3Info);
			intent.putExtra("msg", SystemConfig.PLAYER_MESSAGE.MSG_PLAY);
			mp3Info.setLrcName(mp3Info.getMp3Name().substring(0,mp3Info.getMp3Name().length()-4)+".lrc");
			Mp3PlayerActivity.this.startService(intent);
		}
	}

	class PauseClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(Mp3PlayerActivity.this, Mp3PlayerService.class);
			intent.putExtra("mp3info", mp3Info);
			intent.putExtra("msg", SystemConfig.PLAYER_MESSAGE.MSG_PAUSE);
			Mp3PlayerActivity.this.startService(intent);
			
		}
	}

	class StopClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(Mp3PlayerActivity.this, Mp3PlayerService.class);
			intent.putExtra("mp3info", mp3Info);
			intent.putExtra("msg", SystemConfig.PLAYER_MESSAGE.MSG_STOP);
			Mp3PlayerActivity.this.startService(intent);
		}
	}


	private IntentFilter getIntentFilter(){
		if(filter==null){
			filter=new IntentFilter();
			filter.addAction(SystemConfig.ACTION.LRC_MESSAGE_ACTION);
		}
		return filter;
	}
	
	class lrcMessageBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			String lrcmessage=intent.getStringExtra("lrcmessage");
			lrcview.setText(lrcmessage);
		}
		
		
	}
}
